package com.mininglamp.drools;

import com.my.drools.TestDroolsService;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.StatelessKieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class KieSessionFactory implements Serializable {

	private static Logger logger = LoggerFactory.getLogger(KieSessionFactory.class);

	private static StatelessKieSession statelessKieSession;
	/*ksessionName来源于kmodule.xml(设置规则文件与KSessionName)*/
	public static StatelessKieSession KieSession(String ksessionName) {
		/*创建Session对象*/
		if(statelessKieSession==null){
			logger.info("trying to get a KieServices...");
			KieServices kieServices = KieServices.Factory.get();
			logger.info("trying to get a KieContainer...");
			KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
			logger.info("trying to create a KieSession...");
			statelessKieSession = kieClasspathContainer.newStatelessKieSession(ksessionName);

			TestDroolsService service = TestDroolsService.Factory.get();
			service.done();

		}
		return statelessKieSession;
	}
}
